<template>
  <el-dialog :title="title" :visible.sync="show" append-to-body :close-on-click-modal="false" width="1200px" @closed="closed">
    <el-table :data="list" size="small">
      <el-table-column label="所属部门" prop="showDeptName" show-overflow-tooltip width="150" />
      <el-table-column label="项目" prop="projectName" show-overflow-tooltip width="150" />
      <el-table-column label="项目(检查口径)" prop="inpectionCaliber" show-overflow-tooltip width="120" />
      <el-table-column label="施工单位" prop="constructionName" show-overflow-tooltip width="100" />
      <el-table-column label="批次" prop="phaseName" align="center" show-overflow-tooltip width="100" />
      <el-table-column label="检查单位" align="center" min-width="80">
        <dict-tag slot-scope="{ row }" :options="dict.type.cq_eval_rdc_type" :value="row.rdcType" />
      </el-table-column>
      <el-table-column label="交付类型" align="center" min-width="100">
        <dict-tag slot-scope="{ row }" :options="dict.type.cq_eval_delivery_type" :value="row.deliveryTypes" />
      </el-table-column>
      <el-table-column v-for="item in checkItemList" :key="item.chkitemId" :label="item.chkitemName" align="center" min-width="70">
        <template v-if="shareItemCode.some(code => code === item.chkitemCode)" slot-scope="{ row }">
          <span class="default-level" :class="row | scoreClass(item.chkitemCode, deliveryType.JZMP)">{{ row | scoreName(item.chkitemCode, deliveryType.JZMP, dict.type.cq_eval_result_enum) }}</span>
        </template>
        <el-table-column v-if="!shareItemCode.some(code => code === item.chkitemCode)" label="精装" align="center" min-width="60">
          <template slot-scope="{ row }">
            <span class="default-level" :class="row | scoreClass(item.chkitemCode, deliveryType.JZ)">{{ row | scoreName(item.chkitemCode, deliveryType.JZ, dict.type.cq_eval_result_enum) }}</span>
          </template>
        </el-table-column>
        <el-table-column v-if="!shareItemCode.some(code => code === item.chkitemCode)" label="毛坯" align="center" min-width="60">
          <template slot-scope="{ row }">
            <span class="default-level" :class="row | scoreClass(item.chkitemCode, deliveryType.MP)">{{ row | scoreName(item.chkitemCode, deliveryType.MP, dict.type.cq_eval_result_enum) }}</span>
          </template>
        </el-table-column>
      </el-table-column>
      <el-table-column label="检查日期" align="center" prop="checkDate" min-width="100" />
      <el-table-column label="锁定状态" align="center" width="70">
        <el-tag slot-scope="{ row }" size="small" :type="row.isLockout ? 'primary' : 'info'">{{ row.isLockout ? '锁定' : '未锁' }}</el-tag>
      </el-table-column>
    </el-table>

    <div slot="footer">
      <el-button type="primary" @click="onConfirm">继续录入</el-button>
      <el-button @click="show = false">取消录入</el-button>
    </div>
  </el-dialog>
</template>
<script>
// 相似数据确认
import { shareItemCode, deliveryType, CQ_EVAL_RESULT_ENUM as RESULT } from '@/utils/baseData'
export default {
  dicts: ['cq_eval_rdc_type', 'cq_eval_delivery_type'],
  filters: {
    scoreClass(item = {}, chkitemCode, deliveryType) {
      let val = ''
      if (item && item.scores) {
        for (let i = 0; i < item.scores.length; i++) {
          if (item.scores[i].chkitemCode === chkitemCode && deliveryType.includes(item.scores[i].deliveryType)) {
            val = item.scores[i].scoreResult
          }
        }
      }
      if (val === RESULT.EXCELLENT) return 'green'
      if (val === RESULT.VALID) return 'blue'
      if (val === RESULT.UNVALID) return 'red'
      return ''
    },
    scoreName(item = {}, chkitemCode, deliveryType, dicts = []) {
      let val = '/'
      if (item && item.scores) {
        for (let i = 0; i < item.scores.length; i++) {
          if (item.scores[i].chkitemCode === chkitemCode && deliveryType.includes(item.scores[i].deliveryType)) {
            if (item.scores[i].scoreInput) {
              val = `${item.scores[i].scoreInput}`
              dicts.forEach(item => {
                if (val === item.value) {
                  val = item.label
                }
              })
            }
          }
        }
      }
      return val
    }
  },
  data() {
    return {
      shareItemCode,
      deliveryType,
      show: false,
      hasRdcType: false,
      checkItemList: [],
      list: []
    }
  },
  computed: {
    title() {
      return `当前已有${this.list.length}条相似的成绩，是否要继续录入该成绩？`
    }
  },
  methods: {
    closed() {
      this.hasRdcType = false
      this.checkItemList = []
      this.list = []
    },
    open({ checkItemList = [], data = {} }) {
      this.hasRdcType = !!this.$route.query?.rdcType
      this.checkItemList = checkItemList

      // 处理列表数据
      const list = data.duplicateList || []
      const deptName = i => [i.deptName1, i.deptName2, i.deptName3].filter(i => i).join(' - ')
      list.map(i => (i.showDeptName = deptName(i)))
      this.list = list

      this.show = true
    },
    onConfirm() {
      this.show = false
      this.$emit('confirm')
    }
  }
}
</script>
<style lang="scss" scoped>
.default-level {
  color: #909399;
}
.green {
  color: #67c23a;
}
.blue {
  color: #409eff;
}
.red {
  color: #f56c6c;
}
</style>
